/**
 * Universita' degli Studi di Palermo, 2010-11
 */
package org.unipa.bn.model.alicebot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Map;
import java.util.UUID;

import org.unipa.botplatform.model.Bot;


/**
 * An AliceWebBot is a Bot manager for Alice 
 * chat robot (<code>http://www.alicebot.org/about.html</code>), whose 
 * abilities are available through the Internet.
 * <br>
 * We use a modified version of the original bot.
 * 
 * @author Giuseppe Miceli
 *
 */
public class AliceWebBot implements Bot {

	private String url;

	private String name;

	private String initString;

	public String getName() {
		return this.name;
	}

	public Map<Object, Object> getProperties(String sessionId) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object getProperty(String sessionId, Object propertyName) {
		// TODO Auto-generated method stub
		return null;
	}

	public String interactWithBot(String sessionId, Object interaction) {

		String phrase = (String)interaction;

		URLConnection conn;
		URL dest;

		String answer = null;

		try {    
			phrase = URLEncoder.encode(phrase, "UTF-8");
			String u = this.url + "talkToBot?phrase=" + phrase;  
			if (sessionId != null) u += "&userId=" + sessionId;

			dest = new URL(u);
			conn = dest.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

			answer = in.readLine().trim();
			String add = null;
			while ((add = in.readLine()) != null){
				answer += " " + add.trim();
			}
			answer = answer.trim();

		} catch (MalformedURLException e) {
			e.printStackTrace();			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return answer;
	}

	public String newSession() {
		return UUID.randomUUID().toString();
	}

	public void setName(String botName) {
		this.name = botName;
	}

	public Object setProperty(String sessionId, Object propertyName, Object propertyValue) {

		URLConnection conn;
		URL dest;
		String plugin = (String) propertyName;
		String value = (String) propertyValue;
		Boolean ret = false;
		try {
			String phrase = value.equalsIgnoreCase("ON") ? "PLUGINACTIVATION" : "PLUGINDEACTIVATION";
			phrase += " " + plugin;
			phrase = URLEncoder.encode(phrase, "UTF-8");

			String u = this.url + "talkToBot?phrase=" + phrase + "&userId=" + UUID.randomUUID().toString();

			dest = new URL(u);
			conn = dest.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

			ret = Boolean.parseBoolean(in.readLine().trim());

		} catch (MalformedURLException e) {
			e.printStackTrace();			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ret;
	}


	/**
	 * @param url the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	public String startInteraction(String sessionId) {
		return startInteraction(sessionId, initString);
	}

	public String startInteraction(String sessionId, Object initialization) {
		return interactWithBot(sessionId, initialization);
	}

	/**
	 * @param initString the initString to set
	 */
	public void setInitString(String initString) {
		this.initString = initString;
	}

	public Object closeSession(String sessionId) {
		// TODO Auto-generated method stub
		return null;
	}
}
